{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FY4A AGRI L1 Reader Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Support Files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "FY4A AGRI data in NetCDF format.\n",
    "\n",
    "Both Full DISK and regional images are supported.\n",
    "\n",
    "Example filenames:\n",
    "\n",
    "Full DISK:\n",
    "    \n",
    "    FY4A-_AGRI--_N_DISK_1047E_L1-_FDI-_MULT_NOM_20190807060000_20190807061459_4000M_V0001.HDF\n",
    "\n",
    "REGC:\n",
    "\n",
    "    FY4A-_AGRI--_N_REGC_1047E_L1-_FDI-_MULT_NOM_20190807045334_20190807045750_1000M_V0001.HDF\n",
    "\n",
    "*Full disk scans are identified by DISK , regional scans by REGC.*\n",
    "\n",
    "Data Links:\n",
    "\n",
    "    Real Time Data Service (30 days) and Introduction Files:\n",
    "        https://fy4.nsmc.org.cn/data/en/data/realtime.html\n",
    "    History data (2018-03-12 -- ):\n",
    "        http://satellite.nsmc.org.cn/PortalSite/Data/Satellite.aspx\n",
    "    FY4A official weather application platform:\n",
    "        http://rsapp.nsmc.org.cn/geofy/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Calibration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You have three options:\n",
    "\n",
    "1. The raw detector counts (All channels)\n",
    "\n",
    "2. Reflectance (C01 - C06)\n",
    "\n",
    "3. Radiance and Brightness Temperature (C07 - C14)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Installation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "$ conda install -c conda-forge satpy\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loading data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, glob\n",
    "from satpy.scene import Scene"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['C01',\n",
       " 'C02',\n",
       " 'C03',\n",
       " 'C04',\n",
       " 'C05',\n",
       " 'C06',\n",
       " 'C07',\n",
       " 'C08',\n",
       " 'C09',\n",
       " 'C10',\n",
       " 'C11',\n",
       " 'C12',\n",
       " 'C13',\n",
       " 'C14',\n",
       " 'satellite_azimuth_angle',\n",
       " 'satellite_zenith_angle',\n",
       " 'solar_azimuth_angle',\n",
       " 'solar_glint_angle',\n",
       " 'solar_zenith_angle']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# load FY4A filenames\n",
    "filenames = glob.glob('/xin/data/FY4A/20190807/FY4A-_AGRI*4000M_V0001.HDF')\n",
    "\n",
    "# create the scene object\n",
    "scn = Scene(filenames, reader='agri_l1')\n",
    "\n",
    "# check available channels\n",
    "scn.available_dataset_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# take the ir channel as example\n",
    "ir_channel = 'C12'\n",
    "scn.load([ir_channel], generate=False, calibration='brightness_temperature')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate the full disk image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# display in notebook\n",
    "scn.show(ir_channel)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='./figures/agri_C12.png'>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# save to file\n",
    "# scn.save_dataset(ir_channel, filename='{sensor}_{name}.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate the full disk image (True color)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Too many possible datasets to load for 3.9\n",
      "Too many possible datasets to load for 3.9\n",
      "Too many possible datasets to load for DatasetID(name=None, wavelength=3.9, resolution=None, polarization=None, calibration=None, level=None, modifiers=())\n",
      "Too many possible datasets to load for 3.9\n",
      "Too many possible datasets to load for 3.9\n",
      "Too many possible datasets to load for DatasetID(name=None, wavelength=3.9, resolution=None, polarization=None, calibration=None, level=None, modifiers=())\n",
      "Too many possible datasets to load for 3.9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['ash',\n",
       " 'dust',\n",
       " 'fog',\n",
       " 'green',\n",
       " 'green_snow',\n",
       " 'ir108_3d',\n",
       " 'ir_cloud_day',\n",
       " 'natural_color',\n",
       " 'natural_color_sun',\n",
       " 'overview',\n",
       " 'overview_sun',\n",
       " 'true_color']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get a list of all available composites for the current scene\n",
    "scn.available_composite_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Required file type 'agri_l1_4000m_geo' not found or loaded for 'solar_zenith_angle'\n",
      "Required file type 'agri_l1_4000m_geo' not found or loaded for 'satellite_zenith_angle'\n",
      "Required file type 'agri_l1_4000m_geo' not found or loaded for 'satellite_azimuth_angle'\n",
      "Required file type 'agri_l1_4000m_geo' not found or loaded for 'solar_azimuth_angle'\n",
      "Inconsistent sensor/satellite input - sensor set to agri\n",
      "Inconsistent sensor/satellite input - sensor set to agri\n"
     ]
    }
   ],
   "source": [
    "# Beware that this step might need much memory available on the processing machine (depending on the number of cpu cores)\n",
    "# You can check the link of FAQ about memory:\n",
    "#    https://satpy.readthedocs.io/en/latest/faq.html\n",
    "\n",
    "composite = 'true_color'\n",
    "scn.load([composite])\n",
    "scn.show(composite)\n",
    "# scn.save_dataset(composite, filename='{sensor}_{name}.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='./figures/agri_true_color.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generate the composite on a Region of Interest"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I take the typhoon LEKIMA as an example.\n",
    "\n",
    "We can define a map-projection and a sub area, and project the data on this area.\n",
    "\n",
    "`Pyresample` can be used to define the area easily.\n",
    "\n",
    "This definition can also be put in the `area.yaml` configuration file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyresample import get_area_def\n",
    "\n",
    "area_id = 'lekima'\n",
    "\n",
    "x_size = 549\n",
    "y_size = 499\n",
    "area_extent = (-1098006.560556, -967317.140452, 1098006.560556, 1026777.426728)\n",
    "projection = '+proj=laea +lat_0=19.0 +lon_0=128.0 +ellps=WGS84'\n",
    "description = \"Typhoon Lekima\"\n",
    "proj_id = 'laea_128.0_19.0'\n",
    "\n",
    "areadef = get_area_def(area_id, description, proj_id, projection,x_size, y_size, area_extent)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can generate the area easily by [coord2area_def.py](https://github.com/pytroll/satpy/blob/master/utils/coord2area_def.py)\n",
    "\n",
    "Here's the output of `python coord2area_def.py lekima_4km laea 10 28 118 138 4`:\n",
    "\n",
    "```\n",
    "lekima_4km:\n",
    "  description: lekima_4km\n",
    "  projection:\n",
    "    proj: laea\n",
    "    ellps: WGS84\n",
    "    lat_0: 19.0\n",
    "    lon_0: 128.0\n",
    "  shape:\n",
    "    height: 499\n",
    "    width: 549\n",
    "  area_extent:\n",
    "    lower_left_xy: [-1098006.560556, -967317.140452]\n",
    "    upper_right_xy: [1098006.560556, 1026777.426728]\n",
    "```\n",
    "\n",
    "Now, you can add the configuration to `$PPP_CONFIG_DIR/areas.yaml` and use it directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you have added it to areas.yaml, you can use the name directly:\n",
    "os.environ['PPP_CONFIG_DIR'] = '/yin_raid/xin/satpy_config/'\n",
    "lekima_scene = scn.resample('lekima_4km')\n",
    "\n",
    "# Otherwise, you need to use the areadef defined above:\n",
    "# lekima_scene = scn.resample(areadef)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "lekima_scene.show(composite)\n",
    "# lekima_scene.save_dataset(composite, filename='{sensor}_{name}_resampled.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='./figures/agri_true_color_resampled.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to generate pictures with specific colormap like the figure below,\n",
    "\n",
    "please check another notebook about `enhancement`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src='./figures/agri_C12_resampled_colorize.png'>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
